package Client.GUI.Popups;

import Client.Net.*;
import Client.*;
import Library.Events.TimeEvent;
import Library.Game.*;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.*;
import java.util.List;

/**
 *
 */
public class popAttackResult extends AbstractPopup
{
    protected LandInfo m_AttackingLand;
    protected LandInfo m_DefendingLand;
    
    /**
     * Creates new form popAttackResult
     */
    public popAttackResult(ICommunication communication, IPopupContainer container,
	    LandInfo attLand, List<Integer> attDices, int lostAttArmies, 
	    LandInfo defLand, List<Integer> defDices, int lostDefArmies, 
	    IPlayer attacker, IPlayer defender,
	    boolean isAttacker) throws RemoteException


    {
	super(communication, container);
	initComponents();
	m_AttackingLand = attLand;
	m_DefendingLand = defLand;
	
	String defname = "Unknown";
        String attname = "Unknown";
	try
	{
	    Color attClr = PlayerColor.getColor(attacker.getColor());
	    Color defClr = PlayerColor.getColor(defender.getColor());
	    lblAttackerWho.setForeground(attClr);
	    lblAttackerLand.setForeground(attClr);
	    lblAttackerArmies.setForeground(attClr);
	    lblAttackerBar.setForeground(attClr);
	    lblAttackerResult.setForeground(attClr);
            lblAttackerDice.setForeground(attClr);
            lblWinner.setForeground(attClr);
	    lblDefenderWho.setForeground(defClr);
	    lblDefenderLand.setForeground(defClr);
	    lblDefenderArmies.setForeground(defClr);
	    lblDefenderBar.setForeground(defClr);
	    lblDefenderResult.setForeground(defClr);
            lblDefenderDice.setForeground(defClr);
	    lblTimeRemaining.setForeground(attClr);
	    lblTimeRemainingText.setForeground(attClr);
	    defname = m_DefendingLand.m_Player.getName();
            attname = m_AttackingLand.m_Player.getName();
            lblWinner.setVisible(false);
	    if (isAttacker)
	    {
		lblTimeRemaining.setForeground(attClr);
		lblTimeRemainingText.setForeground(attClr);
	    }
	    else
	    {
		lblTimeRemaining.setForeground(defClr);
		lblTimeRemainingText.setForeground(defClr);
                btnAttack.setVisible(false);
	    }
	}
	catch (RemoteException ex)
	{
	    ex.printStackTrace();
	}
        
	lblAttackerWho.setText("Attacker (" + attname + ")" );
	lblAttackerLand.setText(m_AttackingLand.getName());
	lblAttackerArmies.setText((m_AttackingLand.getAmountOfArmies()) + " soldiers stationed");
	lblAttackerLost.setText(lostAttArmies + " soldiers lost");
	lblAttackerResult.setText((m_AttackingLand.getAmountOfArmies()-lostAttArmies) + " soldiers remaining");
        lblAttackerDice.setText("Thrown dice" + attDices.toString());
        lblDefenderWho.setText("Defender (" + defname + ")");
	lblDefenderLand.setText(defLand.getName());
	lblDefenderArmies.setText((defLand.getAmountOfArmies()) + " soldiers stationed");
	lblDefenderLost.setText(lostDefArmies + " soldiers lost");
	lblDefenderResult.setText((defLand.getAmountOfArmies()-lostDefArmies) + " soldiers remaining");
        lblDefenderDice.setText("Thrown dice" + defDices.toString());
        if((defLand.getAmountOfArmies()-lostDefArmies)<=0)
        {
            lblWinner.setText(attname + " has won "+ defLand.getName() );
            lblWinner.setVisible(true);
             btnAttack.setVisible(false);
        }
       
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnCancel = new javax.swing.JButton();
        btnAttack = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        lblAttackerLand = new javax.swing.JLabel();
        lblAttackerArmies = new javax.swing.JLabel();
        lblAttackerWho = new javax.swing.JLabel();
        lblAttackerBar = new javax.swing.JLabel();
        lblAttackerResult = new javax.swing.JLabel();
        lblAttackerLost = new javax.swing.JLabel();
        lblAttackerDice = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        lblDefenderLand = new javax.swing.JLabel();
        lblDefenderArmies = new javax.swing.JLabel();
        lblDefenderWho = new javax.swing.JLabel();
        lblDefenderBar = new javax.swing.JLabel();
        lblDefenderResult = new javax.swing.JLabel();
        lblDefenderLost = new javax.swing.JLabel();
        lblDefenderDice = new javax.swing.JLabel();
        lblTimeRemainingText = new javax.swing.JLabel();
        lblTimeRemaining = new javax.swing.JLabel();
        lblWinner = new javax.swing.JLabel();

        btnCancel.setText("OK");
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });

        btnAttack.setText("Attack again");
        btnAttack.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAttackActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 24));
        jLabel1.setText("Battlefield results");

        lblAttackerLand.setText("Land");

        lblAttackerArmies.setText("Armies");

        lblAttackerWho.setText("Who");

        lblAttackerBar.setText("----------------------------");

        lblAttackerResult.setText("Result");

        lblAttackerLost.setText("Lost");

        lblAttackerDice.setText("Dice");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblAttackerWho)
                    .addComponent(lblAttackerLand)
                    .addComponent(lblAttackerArmies)
                    .addComponent(lblAttackerLost)
                    .addComponent(lblAttackerResult)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(lblAttackerBar, javax.swing.GroupLayout.DEFAULT_SIZE, 151, Short.MAX_VALUE))
                    .addComponent(lblAttackerDice))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(lblAttackerWho)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblAttackerLand)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblAttackerArmies)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblAttackerLost)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblAttackerDice)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 13, Short.MAX_VALUE)
                .addComponent(lblAttackerBar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblAttackerResult)
                .addContainerGap())
        );

        lblDefenderLand.setText("Land");

        lblDefenderArmies.setText("Armies");

        lblDefenderWho.setText("Who");

        lblDefenderBar.setText("----------------------------");

        lblDefenderResult.setText("Result");

        lblDefenderLost.setText("Lost");

        lblDefenderDice.setText("Dice");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblDefenderWho)
                    .addComponent(lblDefenderLand)
                    .addComponent(lblDefenderArmies)
                    .addComponent(lblDefenderLost)
                    .addComponent(lblDefenderResult)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(lblDefenderBar, javax.swing.GroupLayout.DEFAULT_SIZE, 146, Short.MAX_VALUE))
                    .addComponent(lblDefenderDice))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(lblDefenderWho)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblDefenderLand)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblDefenderArmies)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblDefenderLost)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 8, Short.MAX_VALUE)
                .addComponent(lblDefenderDice)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(lblDefenderBar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblDefenderResult)
                .addContainerGap())
        );

        lblTimeRemainingText.setText("Time Remaining (Round):");

        lblTimeRemaining.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        lblTimeRemaining.setText("0 seconds");

        lblWinner.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(73, 73, 73)
                .addComponent(jLabel1)
                .addContainerGap(80, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addComponent(btnCancel)
                .addGap(150, 150, 150)
                .addComponent(btnAttack)
                .addGap(32, 32, 32))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblWinner, javax.swing.GroupLayout.DEFAULT_SIZE, 132, Short.MAX_VALUE)
                .addGap(79, 79, 79)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lblTimeRemaining, javax.swing.GroupLayout.DEFAULT_SIZE, 132, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(lblTimeRemainingText)
                        .addGap(22, 22, 22))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lblTimeRemainingText)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblTimeRemaining, javax.swing.GroupLayout.DEFAULT_SIZE, 30, Short.MAX_VALUE)
                        .addGap(30, 30, 30))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lblWinner, javax.swing.GroupLayout.DEFAULT_SIZE, 74, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCancel)
                    .addComponent(btnAttack))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnAttackActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAttackActionPerformed
        m_Container.switchPopup(new popAttack(m_Communication, m_Container, m_AttackingLand, m_DefendingLand));
    }//GEN-LAST:event_btnAttackActionPerformed

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
	m_Container.disposePopups();
    }//GEN-LAST:event_btnCancelActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAttack;
    private javax.swing.JButton btnCancel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JLabel lblAttackerArmies;
    private javax.swing.JLabel lblAttackerBar;
    private javax.swing.JLabel lblAttackerDice;
    private javax.swing.JLabel lblAttackerLand;
    private javax.swing.JLabel lblAttackerLost;
    private javax.swing.JLabel lblAttackerResult;
    private javax.swing.JLabel lblAttackerWho;
    private javax.swing.JLabel lblDefenderArmies;
    private javax.swing.JLabel lblDefenderBar;
    private javax.swing.JLabel lblDefenderDice;
    private javax.swing.JLabel lblDefenderLand;
    private javax.swing.JLabel lblDefenderLost;
    private javax.swing.JLabel lblDefenderResult;
    private javax.swing.JLabel lblDefenderWho;
    private javax.swing.JLabel lblTimeRemaining;
    private javax.swing.JLabel lblTimeRemainingText;
    private javax.swing.JLabel lblWinner;
    // End of variables declaration//GEN-END:variables

    protected ActionListener onTurnTimerEvent = new ActionListener()
    {
	@Override
	public void actionPerformed(ActionEvent e)
	{
	    try
	    {
		TimeEvent ev = (TimeEvent)e;
		int time = ev.getTime();
		int timetotal = ev.getTotalTime();
		int hrs = time / 3600;
		int temp = time - (3600 * hrs);
		int mins = temp / 60;
		int secs = temp - (mins * 60);
		lblTimeRemaining.setText(String.format("%02d:%02d:%02d", hrs, mins, secs));
	    }
	    catch (Exception ex)
	    {
		ex.printStackTrace();
	    }
	}
    };
    
    @Override
    protected void register()
    {
	m_Communication.getService().onTurnTimerEvent.register(onTurnTimerEvent);
    }
    
    @Override
    protected void unregister()
    {
	m_Communication.getService().onTurnTimerEvent.unregister(onTurnTimerEvent);
    }
}
